package com.sw.small_world.fragment.home.encyclopedia;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.sw.small_world.R;
import com.sw.small_world.activity.MainActivity;
import com.sw.small_world.fragment.base.BaseFragment;
import com.sw.small_world.utils.ConstantUtils;
import com.sw.small_world.utils.StartUtils;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/3/16.
 */

public class EncyclopediaFragment extends BaseFragment {


    @Bind(R.id.iv_hot_one)
    ImageView ivHotOne;
    @Bind(R.id.tv_hot_one)
    TextView tvHotOne;
    @Bind(R.id.iv_hot_two)
    ImageView ivHotTwo;
    @Bind(R.id.tv_hot_two)
    TextView tvHotTwo;
    @Bind(R.id.iv_hot_three)
    ImageView ivHotThree;
    @Bind(R.id.tv_hot_three)
    TextView tvHotThree;
    @Bind(R.id.gridView)
    GridView gridView;
    private MainActivity activity;
    ArrayList<String> typeList = new ArrayList<>();
    private MyAdapter adapter;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        activity = (MainActivity) getActivity();
        activity.tvTitle.setText("职场百科");
    }

    @Override
    protected Object requestData() {
        typeList.add("1111");
        typeList.add("1111");
        typeList.add("1111");
        typeList.add("1111");
        typeList.add("1111");
        typeList.add("1111");
        typeList.add("1111");
        typeList.add("1111");
        return ConstantUtils.STATE_SUCCESSED;
    }

    @Override
    protected View getSuccessView() {
        View view = View.inflate(activity, R.layout.fragment_encyclopedia, null);
        ButterKnife.bind(this, view);
        adapter = new MyAdapter();
        gridView.setAdapter(adapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                StartUtils.startActivityById(activity, ConstantUtils.ENCYCLOPEDIA_LIST);
            }
        });
        return view;
    }

    @OnClick({R.id.iv_hot_one, R.id.iv_hot_two, R.id.iv_hot_three})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_hot_one:
                StartUtils.startActivityById(activity, ConstantUtils.ENCYCLOPEDIA_LIST);
                break;
            case R.id.iv_hot_two:
                StartUtils.startActivityById(activity, ConstantUtils.ENCYCLOPEDIA_LIST);
                break;
            case R.id.iv_hot_three:
                StartUtils.startActivityById(activity, ConstantUtils.ENCYCLOPEDIA_LIST);
                break;
        }
    }

    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return typeList.size();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = View.inflate(activity, R.layout.item_encyclopedia_type, null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            return convertView;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        class ViewHolder {
            @Bind(R.id.iv_type)
            ImageView ivType;
            @Bind(R.id.tv_type)
            TextView tvType;

            ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
